import { defineConfig ,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import postCssPxToRem from 'postcss-pxtorem'
import tailwindcss from "tailwindcss"
import autoprefixer from "autoprefixer"
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import viteCompression from 'vite-plugin-compression';
import vueSetupExtend from 'vite-plugin-vue-setup-extend'
import Components from 'unplugin-vue-components/vite';
import { VarletImportResolver } from '@varlet/import-resolver'
// https://vitejs.dev/config/
export default defineConfig(({command, mode})=>{
const env = loadEnv(mode, process.cwd());
  console.log(env.VITE_PRXY_BASEURL);
  return {
   
    plugins: [
     
      Components({
        resolvers: [VarletImportResolver()],
        dirs: ['src/components'],
        // dts: 'src/components.d.ts',
        extensions: ['vue', 'jsx', 'tsx', 'ts', 'js'],
        // resolvers: [ElementPlusResolver()],
        
      }),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]',
      }),
      vue(),
      AutoImport({
        resolvers: [VarletImportResolver({ autoImport: true })],
        // 可以自定义文件生成的位置，默认是根目录下，使用ts的建议放src目录下
        // dts: 'src/auto-imports.d.ts',
        imports: ['vue', 'vue-router'],
        // resolvers: [ElementPlusResolver({
        //   importStyle: "sass",
        // })],
      }),
      viteCompression({
        verbose: true, // 默认即可
        disable: false, //开启压缩(不禁用)，默认即可
        deleteOriginFile: false, //删除源文件
        threshold: 10240, //压缩前最小文件大小
        algorithm: 'gzip', //压缩算法
        ext: '.gz', //文件类型
      }),
      vueSetupExtend(),
    ]
    ,
    resolve: {
      alias: {
        '@': resolve(__dirname, './src'), // 设置 `@` 指向 `src` 目录
      }
    },
    css: {
    
      postcss: {
        plugins: [
          tailwindcss, autoprefixer,
          postCssPxToRem({
            // 自适应，px>rem转换
            rootValue: 37.5, // 按照自己的设计稿修改 375/10
            unitPrecision: 5, // 保留到5位小数
            propList: ['*'], // 需要转换的属性，这里选择全部都进行转换
            selectorBlackList: ['norem'], // 过滤掉norem-开头的class，不进行rem转换，这个内容可以不写
          }),
        ],
      },
      preprocessorOptions: {
        scss: {
          additionalData:'@import "@/assets/styles/variables.scss";'
        }
  
      },
    },
    build: {
      outDir: 'wangzhuan'
    },
    base: './',
    server: {
      host: '0.0.0.0',
      port: 1000, // 设置服务启动端口号
      open: false, // 设置服务启动时是否自动打开浏览器
      cors: true, // 允许跨域
      hmr:true,
      // proxy: env.VITE_PROXY_DOMAIN_REAL,
      proxy: {
        '/api' : {
          target: env.VITE_PRXY_BASEURL,
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace('/api', '')
        }
      }
    }
  }
 
 

})
